package com.zks;

import cn.hutool.log.Log;
import cn.hutool.log.StaticLog;
import com.zks.handler.ActionHandler;
import com.zks.setting.Setting;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
* 启动类
* @author zks
* @date 2019/4/13 21:11
* @return
*/
public class MyServer {

    private static final Log log = StaticLog.get();

    public static void main(String[] args) throws Exception {


    }

        public static void start() throws Exception{
        final EventLoopGroup bossGroup= new NioEventLoopGroup(1);
        final EventLoopGroup workGroup=new NioEventLoopGroup();
        try{
            ServerBootstrap serverBootstrap=new ServerBootstrap();
            serverBootstrap.group(bossGroup,workGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().//http 编码解码
                            addLast(new HttpServerCodec()).
                            //把多个消息转换为一个单一的FullHttpRequest或是FullHttpResponse
                            addLast(new HttpObjectAggregator(65536)).
                            //大文件支持
                            addLast(new ChunkedWriteHandler())
                            .addLast(new ActionHandler());
                }
            });
            final Channel channel=serverBootstrap.bind(Setting.getPort()).sync().channel();
            channel.closeFuture().sync();
        }finally {

            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();

        }
    }
}
